package com.example.h1nson.realphotography.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.asyncTask.CaptchaAsynTask;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterNextActivity extends AppCompatActivity {
    private EditText edtEmail,edtCap;
    private Button btnGet,btnReg;
    private int sec;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_next);
        edtEmail=(EditText)findViewById(R.id.regNextEmailEt);
        edtCap=(EditText)findViewById(R.id.regNextCapEdt);
        btnGet=(Button)findViewById(R.id.regGetCap);
        btnReg=(Button)findViewById(R.id.regNextSubBtn);
        init();
    }
    private void init(){
        edtCap.setEnabled(false);
        btnReg.setEnabled(false);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("click getCap");

                if(checkEmail(edtEmail)){
                    btnGet.setEnabled(false);
                    String email = edtEmail.getText().toString();
                    int regType = 0;
                    JSONObject json =new JSONObject();
                    try {
                        json.put("email", email);
                        json.put("regType",regType);
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                    CaptchaAsynTask captchaAsynTask = new CaptchaAsynTask(json);
                    captchaAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
                        @Override
                        public void onDataSuccessfully(Object data) {
                            btnReg.setEnabled(true);
                            edtCap.setEnabled(true);
//                             sec =60;
//                            TimerTask timerTask = new TimerTask() {
//                                @Override
//                                public void run() {
//                                    btnGet.setText(sec+"s");
//                                    sec--;
//                                    if(sec==0){
//                                        btnGet.setText("获取验证码");
//                                        btnGet.setEnabled(true);
//                                        cancel();
//                                    }
//                                }
//                            };
//                            Timer timer = new Timer(true);
//                            timer.schedule(timerTask,1000, 1000);

                        }

                        @Override
                        public void onDataFailed() {
                            btnGet.setEnabled(true);
                            Toast.makeText(RegisterNextActivity.this,"请稍后重试",Toast.LENGTH_SHORT).show();
                        }
                    });
                    captchaAsynTask.execute();
                }
            }
        });
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnReg.setEnabled(false);
                String email = edtEmail.getText().toString();
                int captchaNum = Integer.parseInt(edtCap.getText().toString());
                int regType = 1;
                JSONObject json =new JSONObject();
                try {
                    json.put("email", email);
                    json.put("captcha",captchaNum);
                    json.put("regType",regType);
                }catch (JSONException e){
                    e.printStackTrace();
                }
                CaptchaAsynTask captchaAsynTask = new CaptchaAsynTask(json);
                captchaAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
                    @Override
                    public void onDataSuccessfully(Object data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data.toString());
                            int result = jsonObject.getInt("result");
                            if(result==0){
                                Intent intent = new Intent(RegisterNextActivity.this,RegisterActivity.class);
                                startActivity(intent);
                            }
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onDataFailed() {

                    }
                });
                captchaAsynTask.execute();
            }
        });
    }
    private boolean checkEmail(EditText edtEmail){
        String email = edtEmail.getText().toString();
        if(email==null){
            Toast.makeText(RegisterNextActivity.this,"请输入邮箱",Toast.LENGTH_SHORT).show();
            return false;
        }
        //正则表达式验证邮箱
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher matcher = pattern.matcher(email);
        if(!matcher.matches()){
            Toast.makeText(RegisterNextActivity.this,"无效邮箱",Toast.LENGTH_SHORT).show();
        }
        return matcher.matches();
    }
}
